<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #res{
            width: 200px;
            height: 200px;
            border: solid 2px blue;
        }
    </style>
</head>
<body>

    <div id="res"></div>


    <script>
        let res = document.getElementById('res')
        //绑定键盘按下事件
        window.onkeydown = function(){
            //声明XMLHttpRequest对象
            const xhr = new XMLHttpRequest();
            
            xhr.responseType = "json"  //  => 第二种方法: 返回结果自动转变为json
            //初始化
            xhr.open("POST","http://localhost:8000/server-json")
            //发送
            xhr.send()

            xhr.onreadystatechange = function(){
                if(xhr.readyState == 4){
                    if(xhr.status <= 200 && xhr.status <300){
                        // console.log(JSON.parse(xhr.response))   =>第一种方法: JSON.parse()转换成对象
                        // res.innerHTML = xhr.response
                        console.log(xhr.response)   
                    }
                }
            }
        }







    </script>
    
</body>
</html>